﻿using System;
using System.Net;
using System.Xml;
using FeedsLibrary.Feeds.Abstract;
using FeedsLibrary.Feeds.Atom;
using FeedsLibrary.Feeds.RSS;
using FeedsLibrary.Feeds.RDF;

namespace FeedsLibrary.Feeds
{
    public static class ChannelFactory
    {
        private enum enFeedType
        {
            Atom,
            RSS,
            RDF,
            Unknown
        }

        public static IChannel CreateEmptyFeed()
        {
            return new RssChannel();
        }

        public static IChannel CreateFeed(Uri url, Uri iconUrl, DateTime currentDay, int days, bool tagsOnly)
        {
            //string strUrl = data;
            IChannel channel;

            try
            {
                enFeedType type = DetermineFeedType(url);
                switch ( type )
                {
                    case enFeedType.RSS:
                        channel = new RssChannel(url, iconUrl, currentDay, days, tagsOnly);
                        break;
                    case enFeedType.Atom:
                        channel = new AtomChannel(url, iconUrl, currentDay, days, tagsOnly);
                        break;
                    case enFeedType.RDF:
                        channel = new RDFChannel(url, iconUrl, currentDay, days, tagsOnly);
                        break;
                    default:
                        throw ( new Exception() );
                }
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("Error loading RSS feed - {0}", url.AbsoluteUri), e);
            }

            return channel;
        }

        private static enFeedType DetermineFeedType( Uri sourceUrl )
        {
            XmlDocument rssDoc;

            enFeedType result = enFeedType.Unknown;

            // Create a new XmlTextReader from the specified URL (RSS feed)
            rssDoc = new XmlDocument();
            try
            {
                if ( sourceUrl.AbsoluteUri.StartsWith( @"http://" ))
                {
                    HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( sourceUrl );
                    request.UserAgent = "Bond, James Bond";
                    HttpWebResponse response = ( HttpWebResponse )request.GetResponse();

                    // Load the XML content into a XmlDocument
                    rssDoc.Load( new XmlTextReader( response.GetResponseStream() ) );
                }
                else
                {
                    using (XmlTextReader t = new XmlTextReader(sourceUrl.AbsoluteUri))
                    {
                        rssDoc.Load( t );
                    }
                }
            }
            catch ( Exception )
            {
                return enFeedType.Unknown;
            }

            // Loop for the <rss> tag
            for ( int i = 0; i < rssDoc.ChildNodes.Count; i++ )
            {
                // If it is the rss tag
                if ( rssDoc.ChildNodes[ i ].Name == "rss" )
                {
                    result = enFeedType.RSS;
                    break;
                }
                // If it is the rss tag
                if ( rssDoc.ChildNodes[ i ].Name == "feed" )
                {
                    result = enFeedType.Atom;
                    break;
                }
                // If it is the rss tag
                if (rssDoc.ChildNodes[i].Name == "rdf:RDF")
                {
                    result = enFeedType.RDF;
                    break;
                }
            }
            return result;
        }
    }
}